/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package forumgui;

import TamirsGui.GuiMessage;
import TamirsGui.GuiMessage.MessageAuthorization;
import javax.swing.JOptionPane;

/**
 *
 * @author Roy
 */
public class ViewPostMsg extends javax.swing.JDialog {

    /**
     * Creates new form NewJDialog
     */
    public ViewPostMsg(java.awt.Frame parent, boolean modal,GuiMessage msg) {
        super(parent, modal);
        initComponents();
        reply = null;
        isEdited = false;
        this.isDeleted = false;
        if (msg != null)
        {
            createWindowFromExsitMsg(msg);
        }
        else
        {
            createWindowFromEmpty();
        }
    }

    private void createWindowFromExsitMsg(GuiMessage msg) {
        this.msg = msg;
        this.subjectField.setText(msg.getTitle());
        this.contentField.setText(msg.getBody());
        this.sendButton.setVisible(false);
        boolean isEditable = isEditable(msg.getAuthorization());
        if (!isEditable)
        {
            this.editButton.setVisible(false);
            this.deleteButton.setVisible(false);
        }
        if (msg.getAuthorization() == GuiMessage.MessageAuthorization.guest)
        {
            this.replyButton.setVisible(false);
        }
        this.contentField.setEditable(false);
        this.subjectField.setEditable(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        deleteButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        replyButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        contentField = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        subjectField = new javax.swing.JTextField();
        titleLabel = new javax.swing.JLabel();
        contentLabel = new javax.swing.JLabel();
        sendButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        deleteButton.setText("delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        editButton.setText("edit");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        replyButton.setText("reply");
        replyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                replyButtonActionPerformed(evt);
            }
        });

        exitButton.setText("exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        contentField.setColumns(20);
        contentField.setRows(5);
        jScrollPane2.setViewportView(contentField);

        jScrollPane3.setViewportView(subjectField);

        titleLabel.setText("subject");

        contentLabel.setText("content");

        sendButton.setText("send");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(titleLabel)
                            .addComponent(contentLabel))
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(replyButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sendButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(exitButton)))))
                .addContainerGap(63, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editButton)
                    .addComponent(deleteButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(titleLabel)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(replyButton)
                            .addComponent(exitButton)
                            .addComponent(sendButton)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(contentLabel)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
       this.setVisible(false);
    }//GEN-LAST:event_exitButtonActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
        this.subjectField.setEditable(true);
        this.contentField.setEditable(true);
        this.sendButton.setVisible(true);
        this.editButton.setVisible(false);
        this.replyButton.setVisible(false);
    }//GEN-LAST:event_editButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
      int userChoise =  JOptionPane.showConfirmDialog(null,
                "are you sure you want to delte this message"
                , "delete msg", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
       if (userChoise == JOptionPane.OK_OPTION)
       {
          isDeleted = true;
           
           this.setVisible(false);
       }
       
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
        // TODO add your handling code here:
        String subject = this.subjectField.getText();
        String body = this.contentField.getText();
        if (subject.equals("") && body.equals("" ))
        {
            JOptionPane.showMessageDialog(this,  "you must enter subject or body","Error ",JOptionPane.ERROR_MESSAGE);
            return;
        }
        this.sendEditMsg(subject,body);
        this.setVisible(false);
    }//GEN-LAST:event_sendButtonActionPerformed

    private void replyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_replyButtonActionPerformed

        reply = new Reply(null, true, msg);
        reply.setVisible(true);
        this.setVisible(false);
        
        
        // TODO add your handling code here:
    }//GEN-LAST:event_replyButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea contentField;
    private javax.swing.JLabel contentLabel;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton editButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton replyButton;
    private javax.swing.JButton sendButton;
    private javax.swing.JTextField subjectField;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables

   private GuiMessage msg;
   private boolean isEdited;
   private Reply reply;
   private boolean isDeleted;
   private String subject;
   private String body;
    
    private void sendEditMsg(String subject, String body) {
        
        isEdited = true;
        this.subject = subject;
        this.body = body;
        
    }

    public String getBody() {
        return body;
    }

    public String getSubject() {
        return subject;
    }

    public boolean isDeleted() {
        return isDeleted;
    }

    public boolean isEdited() {
        return isEdited;
    }

    public Reply getReply() {
        return reply;
    }
    
    public static void main(String [] args)
    {
//        ViewPostMsg view = new ViewPostMsg(null, true, new GuiMsg(null, "hello", "מה המצב", GuiMsg.MessageType.postMsg, true));
//        view.setVisible(true);
    }

    private boolean isEditable(MessageAuthorization authorization) {
        switch (authorization)
        {
            case admin:case moderator: case owner :
                return true;
        }
        return false;
    }

    private void createWindowFromEmpty() {
        this.deleteButton.setVisible(false);
        this.editButton.setVisible(false);
        this.replyButton.setVisible(false);
        this.sendButton.setVisible(true);
        
    }
   
    
    
    
}
